# -*- coding: utf-8 -*-
import re
import time

import requests
from lxml import etree
from vip_request_init import vip_request
"""
思路：
request请求url
读取"SNUID.txt"文件到列表snuidList列表 机制，如果用其中的一条sunid出现验证码，就废弃它
读取"keywords.txt" 文件到列表keywordsList，循环请求
写入 "符合SEO的词语.txt" 把筛选出来的词语保存
判断是否存在 serach = re.search('请输入图中的验证码', req.text)
运行逻辑：
循环词语，每次snuid改变要求一个新的request请求，snuid不改变就用session

"""





"""
根据获得的html内容做分类判断
"""
#匹配目标公众号
def matchGZH(content, keyword):
    abc = fr'<em><!--red_beg-->{keyword}<!--red_end--></em></a>'
    # print(content)
    myvalue = re.search(abc,content,re.IGNORECASE)
    print(myvalue)
    if myvalue == None:
        # 丢弃
        # print(f'公众号没找到{keyword}')
        return False
    else:
        #匹配到公众号
        # print(f'公众号-------{keyword}')
        return True
#匹配目标微信
def matchwx(content, keyword):
    #进来的微信词语获得一下对应的公众号名，有名字的匹配下公众号
    def etreeParse(content):
        try:
            ehtml = etree.HTML(content)
            #找到对应的公众号
            searchlist = ehtml.xpath('//*[@id="sogou_vr_11002301_box_0"]/div/div[2]/p[1]/a/text()')
            print('searchlist内容是-------------')
            print(searchlist)
            return searchlist[0]
        except:
            pass
    try:
        wx_gzh = etreeParse(content)   #获得微信号对应的公众号名称
        vip_request.writetxt(str(wx_gzh),'可能用上的词语.txt')
    except Exception as e:
        print(e)
        pass
    abc = fr'<p class="info">微信号：<label name="em_weixinhao">{keyword}</label>'
    myvalue = re.search(abc, content, re.IGNORECASE)
    if myvalue == None:
        # 丢弃# print(f'丢弃{keyword}')
        return False
    else:
        #匹配到微信号# print(f'微信号-------{keyword}')
        return True
#查询关键词，如果有就存到可用list
def matchkeyword(html,keyword):
    if matchwx(html, keyword) or matchGZH(html,keyword):
        print(f"{keyword}存入txt文档ok")
        vip_request.writetxt(keyword,'符合SEO的词语.txt')
    else:
        print(f"废弃词语{keyword}")

def getHtml(session,keyword,url,headers,cookies):
    print(url)
    """
    新建request，内部用session进行请求
    cookie和header都不改变，如果出现验证码就退出该次request,并关闭
    现不用代理，就用本机的ip跑，看看会不会稳定
    """
    print(f'Runtime------{keyword}')
    print(f"1-['{keyword}',{cookies['SNUID']}")
    html = session.get(url=url, headers=headers, cookies=cookies, timeout=(50, 50))
    html.encoding = 'utf-8'
    print(f"2-['{keyword}','{nowtimt}','响应后SNUID'],{session.cookies.get_dict()}")
    print(html.text)
    if 'SNUID' in session.cookies.get_dict():
        if cookies['SNUID'] !=session.cookies.get_dict()['SNUID']:
            print('系统给分配SNUID了')
        else:
            print('一致')
    else:
        print('系统不给snuid了')

    if '请输入图中的验证码' in html.text:    #出现验证码，退出requests
        # session.cookies.clear()
        # session.headers.clear()
        # session.proxies.clear()
        session.close()
        print('session exit')
        return False
    else:                                      #关键词过滤任务
        print(".........")
        matchkeyword(html.text, keyword)
        return True


if __name__ == '__main__':
    # 初始化
    vip_request = vip_request()  # 实例化vip工具类
    keywordsList = vip_request.readtxt('keywords.txt')
    snuidlist = vip_request.readtxt('SNUID.txt')
    # getHtml的参数需要(session, keyword, url, headers, cookies)
    for snuid in snuidlist:  #遍历snuid，一个一个的用，如果失效，则进行下一个循环,每次失效，从新生成新的cookie和headers
        headers = vip_request.get_header()
        cookies = vip_request.getcookie()
        session = requests.session()
        for keyword in keywordsList:  #取得snuid之后，用这个snuid遍历关键词,关键词确定后，进行请求
            url = f'https://weixin.sogou.com/weixin?type=1&s_from=input&query={keyword}&ie=utf8&_sug_=n&_sug_type_='
            cookies['SNUID'] = snuid
            # print(keywordsList)
            try:
                time.sleep(3)
                nowtimt = time.strftime('%Y-%m-%d %H:%M:%S')
                vip_request.writetxt(f"['{keyword}','{nowtimt}','进行到的SNUID'],{snuid}",'Slog.txt')
                Stat = getHtml(session, keyword, url, headers, cookies)  # 请求html，如果出现验证码，就关闭session，下次循环会进入except
                if Stat:
                    keywordsList.remove(keyword)
                    print(keywordsList)
                    continue
                else:
                    break
            except Exception as e:
                nowtimt = time.strftime('%Y-%m-%d %H:%M:%S')
                vip_request.writetxt(f"['{keyword}','{nowtimt}','进行到的SNUID'],{snuid},'出现验证码问题报错'", 'Slog.txt')












